import os

from kivy.uix.boxlayout import BoxLayout

os.environ['KIVY_TEXT'] = 'pil'

from kivy.app import App
from kivy.uix.gridlayout import GridLayout  # 布局
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.core.text import LabelBase, DEFAULT_FONT


'''
资料
kivy入门教程：https://www.jianshu.com/p/f95576c03f5b
kivy入门教程：https://blog.csdn.net/hpwzjz/article/details/89703313
'''
# 注册默认字体
LabelBase.register(DEFAULT_FONT, "../resources/font/simkai.ttf")

class LoginScreen(BoxLayout):

    def __init__(self, **kwargs):
        super(LoginScreen, self).__init__(**kwargs)
        # self.register(name="jt",fn_regular="simkai.ttf")
        self.cols = 2
        self.add_widget(Label(text='用户名'))
        self.username = TextInput(multiline=False)
        self.add_widget(self.username)
        self.add_widget(Label(text='密码'))
        self.password = TextInput(password=True, multiline=False)
        self.add_widget(self.password)

        button = Button(text='登录', size_hint=(.2, .2),
                        pos_hint={"center_x": .5, "center_y": .5})
        button.bind(on_press=self.on_press_button)
        self.add_widget(button) #将按键加入到Layout中


    def on_press_button(self, instance):
        print('你按下了{}键'.format(instance.text))
        print(f'用户名：{self.username.text},密码：{self.password.text}')

class MyApp(App):

    def build(self):
        return LoginScreen()

if __name__ == '__main__':
    MyApp().run()